package org.ddd.specification.catalog;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.ddd.specification.Specification;

/**
 * Template implementation of {@link Specification}. Extend from this class
 * to reduce the number of methods to implement.
 * 
 * @author Jeroen van Schagen
 * @since 29-12-2010
 *
 * @param <T> type of candidates being checked
 */
public abstract class AbstractSpecification<T> implements Specification<T> {

    // Chaining

    /**
     * {@inheritDoc}
     */
    @Override
    public AndSpecification<T> and(Specification<? super T> rhs) {
        return new AndSpecification<T>(this, rhs);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public OrSpecification<T> or(Specification<? super T> rhs) {
        return new OrSpecification<T>(this, rhs);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public NotSpecification<T> not() {
        return new NotSpecification<T>(this);
    }

    // Object behaviour

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

}
